﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Daidalos.Management;
using Daidalos.Exception;

namespace Daidalos.VFS
{
    /// <summary>
    /// 
    /// </summary>
    public class BinaryFile : File
    {
        #region Attributes
        /// <summary>
        /// 
        /// </summary>
        private System.IO.BinaryReader read;

        /// <summary>
        /// 
        /// </summary>
        private System.IO.BinaryWriter write;

        /// <summary>
        /// 
        /// </summary>
        private OpenMode openMode;
        #endregion

        #region Properties
        /// <summary>
        /// 
        /// </summary>
        public OpenMode OpenMode
        {
            get
            {
                return openMode;
            }
            set
            {
                openMode = value;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="openmode"></param>
        public BinaryFile(string name, OpenMode openmode)
        {
            LogManager.Instance.Log("Opening \"" + name + "\" in binary mode");

            System.IO.FileMode mode;
            if(openmode == OpenMode.APPEND)
            {
                mode = System.IO.FileMode.Append;
            }
            else if(openmode == OpenMode.READ)
            {
                mode = System.IO.FileMode.Open;
            }
            else if(openmode == OpenMode.WRITE)
            {
                mode = System.IO.FileMode.Create;
            }
            else 
            {
                mode = System.IO.FileMode.OpenOrCreate;
            }

            System.IO.FileStream file = new System.IO.FileStream(name, mode);

            if (openmode == OpenMode.READ)
            {
                read = new System.IO.BinaryReader(file);
            }
            else if (openmode == OpenMode.WRITE)
            {
                write = new System.IO.BinaryWriter(file);
            }
            else if (openmode == OpenMode.HYBRID)
            {
                read = new System.IO.BinaryReader(file);
                write = new System.IO.BinaryWriter(file);
            }
            LogManager.Instance.Log("  + Successfull");
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public void Close()
        {
            if(read != null)
                read.Close();
            if(write != null)
                write.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public double ReadDouble()
        {
            if (OpenMode == OpenMode.WRITE || OpenMode == OpenMode.APPEND)
                throw new GenericException("File not opened in read or hybrid mode!");

            return read.ReadDouble();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="val"></param>
        public void WriteDouble(double val)
        {
            if (OpenMode == OpenMode.READ)
            {
				throw new GenericException("File not opened in write or append mode!");
            }

            write.Write(val);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public float ReadFloat()
        {
            if (OpenMode == OpenMode.WRITE || OpenMode == OpenMode.APPEND)
				throw new GenericException("File not opened in read or hybrid mode!");

            byte[] floatBytes = read.ReadBytes(4);
            return System.BitConverter.ToSingle(floatBytes, 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="val"></param>
        public void WriteFloat(float val)
        {
            if (OpenMode == OpenMode.READ)
				throw new GenericException("File not opened in write or append mode!");

            write.Write(val);
        }
    }
}
